let loading = false;
loadUsers();
function loadUsers() {
    if (loading) return;
    loading = true;
    $.ajax({
        url: '/api/user.php?type=souser',
        type: 'POST',
        data: {
            uid: uid, // 用户 ID
            page: page, // 当前页码
            keywords: keyword,
        },
        dataType: 'json',
        success: function(response) {
            if (response.status === 200) {
                //console.log(response.message);
                const userList = response.message;
                if(!userList){
                    return;
                }

                userList.forEach(user => {
                    const userHtml = `
                    <div class="row">
                        <div class="col-1 so_userimg">
                            <img src="${user.avatar || '/data/avatar/default.jpg'}" alt="" class="notify_uavatar souser_avatar"/>
                        </div>
                        <div class="col">
                            <div class="n_uname">${user.nickname}</div>
                            <div class="so_ublueid">小蓝书号：${user.blue_id}</div>
                            <div class="so_desc">粉丝・${user.statistics.fans_count} <span class="divider"></span> 笔记・${user.note_num}</div>
                        </div>
                        ${uid !== user.user_id ? `
                        <div class="col-2">
                            <div class="col text-end follow_btn">
                                <button class="${user.isfans ? 'follows_btn' : ''}" 
                                        id="followButton" 
                                        data-fans-id="${uid}" 
                                        data-uid="${user.user_id}">
                                    ${user.isfans ? '已关注' : '关注'}
                                </button>
                            </div>
                        </div>
                        ` : ''}
                    </div>
                `;
                    $('#usersoContainer').append(userHtml);
                });
                page++; // 增加页码
            }
            loading = false; // 加载完成
        },
        error: function() {
            //alert('请求失败，请重试！');
            loading = false; // 加载完成
        }
    });
}

//ajax fans
$(document).on('click', '#followButton', function(event) {
    event.preventDefault();
    event.stopPropagation();
    var $this = $(this);
    var fans_id = $(this).data('fans-id');
    var uid = $(this).data('uid');

    if(!fans_id){
        Qmsg.info('登录后才能关注！');

        // 2秒后跳转到登录页面
        setTimeout(function() {
            window.location.href = '/login.php';
        }, 2000);
        return;
    }

    $.ajax({
        url: '/api/action.php?type=fans',
        method: 'POST',
        data: {
            fans_id: fans_id,
            uid: uid
        },
        dataType: 'json',
        success: function(response) {
            if (response.status === '200') {
                Qmsg.success(response.message);
                $this.addClass('follows_btn');
                $this.text('已关注');
            } else {
                Qmsg.info(response.message);
                $this.removeClass('follows_btn');
                $this.text('关注');
                //alert('操作失败: ' + response.message);
            }
        },
        error: function(xhr, status, error) {
            Qmsg.error(error);
            //alert('请求失败: ' + error);
        }
    });
});


$('#usersoContainer').on('click', '.row', function(event) {
    if (!$(event.target).closest('#followButton').length) {
        const userId = $(this).find('#followButton').data('uid');
        window.open(`/profile.php?id=${userId}`, '_blank');
    }
});

$(window).on('scroll', function() {
    if ($(window).scrollTop() + $(window).height() >= $(document).height() - 50) {
        loadUsers();
    }
    toggleUpButton();
});

//右下角按钮JS
const flashBtn = document.querySelector('.flash_btn');
const flashText = document.getElementById('flash_text');
const upBtn = document.querySelector('.up_btn');
const upText = document.getElementById('up_text');

function showElement(element) {
    element.style.display = 'flex';
}

function hideElement(element) {
    element.style.display = 'none';
}

function toggleUpButton() {
    // 如果页面滚动距离大于 100 像素，则显示按钮，否则隐藏按钮
    if (window.scrollY > 100) {
        upBtn.style.display = 'flex';
    } else {
        upBtn.style.display = 'none';
    }
}

// 事件监听
flashBtn.addEventListener('mouseover', function () {
    showElement(flashText); // 显示 flash_text
});
flashBtn.addEventListener('mouseout', function () {
    hideElement(flashText); // 隐藏 flash_text
});

upBtn.addEventListener('mouseover', function () {
    showElement(upText); // 显示 up_text
});
upBtn.addEventListener('mouseout', function () {
    hideElement(upText); // 隐藏 up_text
});

upBtn.addEventListener('click', function () {
    // 平滑滚动回到页面顶部
    window.scrollTo({
        top: 0,
        behavior: 'smooth'
    });
});

